package com.thirteen.start.vo;

import com.thirteen.start.constant.ResultCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回给前端的VO统一包装在Result中，统一返回给前端的JSON数据层次结构
 *
 * @param <T> 要返回给前端的数据
 * @author hhx
 * @since 2020-8-27 23:22:50
 */
@Data
public class Result<T> implements Serializable {

    private T data;

    private Boolean success;

    private String errCode;

    private String errMsg;


    public static <T> Result<T> create() {
        Result<T> result = new Result<>();
        result.success = true;
        return result;
    }

    public static <T> Result<T> create(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> createFalse(String errCode, String errMsg) {
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setErrCode(errCode);
        result.setErrMsg(errMsg);
        return result;
    }

    public static <T> Result<T> createFalse(String errMsg) {
        Result<T> result = new Result<>();
        result.setErrMsg(errMsg);
        result.setSuccess(false);
        return result;
    }

    public static <T> Result<T> createFalse(ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();
        result.setErrMsg(resultCodeEnum.msg);
        result.setErrCode(resultCodeEnum.code);
        return result;
    }

}
